import 'package:flutter/material.dart';



import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:typed_data';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'dart:convert';
//import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;
import 'package:image/image.dart' as img;
//import 'package:path_provider/path_provider.dart';
//import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:aweb5/Constants.dart';
class FileUtil   {



   static Future<String> saveBase64toGallery1(String base64) async{
     if(base64.contains(",")){
       base64 = base64.split(",")[1];
     }
     Uint8List bytes = Base64Decoder().convert(base64);
     //ByteData byteData =
     final result = await ImageGallerySaver.saveImage(bytes.buffer.asUint8List()); //这个是核心的保存图片的插件
     print(result);   //这个返回值 在保存成功后会返回true
     img.decodeImage(bytes);

     return result;
   }

   static Future<String> saveBase64toGalleryFromUrl(String url) async{


     //ByteData byteData =
     final result = await ImageGallerySaver.saveFile( url ); //这个是核心的保存图片的插件
     print(result);   //这个返回值 在保存成功后会返回true

//     Fluttertoast.showToast(msg: "save$result");
     return result;
   }
// Future<String> get _localPath async {
//   final _path = await getTemporaryDirectory();
//   return _path.path;
// }

// Future<File> get _localFile async {
//   final path = await _localPath;
//
//   return File('$path/counter.txt');
// }

// static Future<String> saveBase64toGallery(String base64) async {
//  // final url = 'https://raw.githubusercontent.com/dart-lang/logos/master/dart/logo/64.png';
//  // final res = await http.get(url);
//
//   if(base64.contains(",")){
//     base64 = base64.split(",")[1];
//   }
//   Uint8List bytes = Base64Decoder().convert(base64);
//   final image = img.decodeImage(bytes);
//
//  // Directory appDocDir = await getApplicationDocumentsDirectory();
//   Directory tempDir = await getTemporaryDirectory();
//   String tempPath = tempDir.path; // 临时文件夹
//  // String appDocPath = appDocDir.path; // 应用文件夹
//
//   final imageFile = File(path.join(tempPath, '${DateTime.now().millisecondsSinceEpoch}.png')); // 保存在应用文件夹内
//   await imageFile.writeAsBytes(img.encodePng(image)); // 需要使用与图片格式对应的encode方法
//
//print(">>>>>>>file:///${imageFile.path}");
//return "file://${imageFile.path}";
//   // 打印各种属性以验证文件保存成功
//   // print(imageFile.path);
//   // print(imageFile.statSync());
// }


  static  Future<String>  findLocalPath(BuildContext context)  async { //这里根据平台获取当前安装目录
//    '/storage/emulated/0/Android/data/com.jijia.jijiacollectgoodthings/files'
//     var a = await getExternalStorageDirectory();
//     '/data/user/0/com.jijia.jijiacollectgoodthings/cache'
//     var b = await getTemporaryDirectory();
//    '/data/user/0/com.jijia.jijiacollectgoodthings/files'
//     var c = await getApplicationSupportDirectory();
//     var d = await getExternalCacheDirectories() ;

//     print("url>>>$a");
//     print("url>>>$b");
//     print("url>>>$c");
//     print("url>>>${getDownloadsDirectory()}");
    final directory = Theme.of(context).platform == TargetPlatform.android
        ? await getExternalStorageDirectory()
//        ? await getTemporaryDirectory()
//        ? await getApplicationSupportDirectory()
        : await getApplicationDocumentsDirectory();
    return directory.path;
  }

  static  Future<String>  clearDirect(String path,BuildContext context)  async { //这里根据平台获取当前安装目录
//    String basicpath = (await FileUtil.findLocalPath(context)) +
//        Constants.savePath;
    final savedDir = Directory(path);
    bool hasExisted = await savedDir.exists();
    if (hasExisted) {
      savedDir.delete();
    }

  }


}